package com.gxnzd.communityelderly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxnzd.communityelderly.commom.BaseContext;
import com.gxnzd.communityelderly.commom.OSS;
import com.gxnzd.communityelderly.commom.R;
import com.gxnzd.communityelderly.dao.ResForumPage;
import com.gxnzd.communityelderly.entity.*;
import com.gxnzd.communityelderly.mapper.*;
import com.gxnzd.communityelderly.service.ForumService;
import com.gxnzd.communityelderly.service.PostLikeService;
import jakarta.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 23234
* @description 针对表【forum】的数据库操作Service实现
* @createDate 2024-11-15 23:40:29
*/
@Service
public class ForumServiceImpl extends ServiceImpl<ForumMapper, Forum>
    implements ForumService{

    @Resource
    private ForumMapper forumMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private CommunityMapper communityMapper;

    @Resource
    private CriticizeMapper criticizeMapper;

    @Resource
    private PostLikeMapper postLikeMapper;



    /**
     * 获取帖子列表
     * @param page
     * @param pageSize
     * @param postTitle
     * @param postStatus
     * @return
     */
    @Override
    public R getListPage(
            Integer page,
            Integer pageSize,
            String postTitle,
            Integer postStatus,
            Long userId) {
        Page<Forum> forumPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Forum> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(postTitle),Forum::getPostTitle,postTitle)
                .orderByDesc(Forum::getCreateTime);
        if(postStatus != null){
            wrapper.eq(Forum::getPostStatus,postStatus);
        }
        forumMapper.selectPage(forumPage,wrapper);
        //要返回的分页
        Page<ResForumPage> forumPageList = new Page<>(page,pageSize);
        ArrayList<ResForumPage> list = new ArrayList<>();
        //需要循环数据将用户信息和社区信息取出来
        for (Forum forum:forumPage.getRecords()) {
            ResForumPage resForumPage = new ResForumPage();
            Employee employee = employeeMapper.selectById(forum.getEmpId());
            //判断员工是否存在
            if(employee == null){
                //删除此帖子
                deletePostItem(forum.getPostId());
                continue;
            }
            Community community = communityMapper.selectById(employee.getCommunityId());
            //判断社区是否存在
            if(community == null){
                deletePostItem(forum.getPostId());
                continue;
            }
            resForumPage.setForum(forum);
            resForumPage.setCommunity(community);
            resForumPage.setEmployee(employee);
            //查询用户是否点赞了这条帖子
            LambdaQueryWrapper<PostLike> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PostLike::getUserId,userId)
                    .eq(PostLike::getPostId,forum.getPostId())
                    .eq(PostLike::getType,1);
            PostLike like = postLikeMapper.selectOne(queryWrapper);
            if(like == null){
                resForumPage.setLike(false);
            }else{
                resForumPage.setLike(true);
            }

            list.add(resForumPage);
        }
        forumPageList.setRecords(list);
        forumPageList.setTotal(forumPage.getTotal());
        forumPageList.setPages(forumPage.getPages());
        forumPageList.setCurrent(forumPage.getCurrent());
        forumPageList.setSize(forumPage.getSize());
        return R.success(forumPageList);
    }

    /**
     * 添加帖子
     * @param forum
     * @return
     */
    @Override
    public R addPost(Forum forum) {
        //判断是否当前是否是超级管理员
        if( BaseContext.getThreadLocal() == 0){
            return R.error("超级管理员不能添加帖子");
        }
        forum.setPostStatus(1);
        forum.setLikeNum(0);
        forum.setCriticizeNum(0);
        forumMapper.insert(forum);
        return R.success("添加成功");
    }

    /**
     * 获取帖子详情
     * @param postId
     * @return
     */
    @Override
    public R getPostDetails(Long postId) {
        Forum forum = forumMapper.selectById(postId);
        Employee employee = employeeMapper.selectById(forum.getEmpId());
        Community community = communityMapper.selectById(employee.getCommunityId());
        ResForumPage forumPage = new ResForumPage();
        forumPage.setForum(forum);
        forumPage.setEmployee(employee);
        forumPage.setCommunity(community);
        return R.success(forumPage);
    }


    /**
     * 修改帖子的状态
     * @param postId
     * @param postStatus
     * @return
     */
    @Override
    public R setPostStatus(Long postId,Integer postStatus) {
        Forum forum = forumMapper.selectById(postId);
        forum.setPostStatus(postStatus);
        forumMapper.updateById(forum);
        return R.success("修改成功");
    }

    /**
     * 获取某个用户的帖子列表
     * @param userId
     * @return
     */
    @Override
    public R getUserForumList(Long userId) {
        LambdaQueryWrapper<Forum> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Forum::getEmpId,userId).orderByDesc(Forum::getCreateUser);
        List<Forum> forums = forumMapper.selectList(wrapper);
        return R.success(forums);
    }

    /**
     * 删除帖子
     * @param postId
     * @return
     */
    @Override
    public R deletePostItem(Integer postId) {
        //帖子删除之后的评论表 喜欢表都要删除关于此帖子的数据
        LambdaQueryWrapper<PostLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PostLike::getPostId,postId);
        postLikeMapper.delete(wrapper);
        LambdaQueryWrapper<Criticize> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Criticize::getPostId,postId);
        criticizeMapper.delete(queryWrapper);
        forumMapper.deleteById(postId);
        return R.success("删除成功");
    }
}




